// pages/mine/storage/index.js
const app = getApp()
const db = wx.cloud.database()
const _ = db.command

Page({

    /**
     * 页面的初始数据
     */
    data: {
        info:"",
        showWJJ: false,
        showpop: false,
        showpop1: false,
        dataList:[1,1,1],
        folderList:[],
        addindex:[]
    },

    toback() {wx.switchTab({
      url: '../index/index',
    })},
    showWJ(){ this.setData({ showWJJ: true }) },
    showW(){ this.setData({ showWJJ: false }) },
    showPopup(e) { 
        let del_index = e.currentTarget.dataset.index;
        this.setData({ showpop: true,findex:del_index }) 
    },
    showPopup1(e) {
        let del_index = e.currentTarget.dataset.index;
        this.setData({ showpop1: true,
            del_index: del_index,
            del_indexx: del_index
        });
    },
    showadd(e) {
        let that = this
        let del_index = e.currentTarget.dataset.index;
        let addindex = that.data.addindex
        addindex[del_index] = 1
        this.setData({ 
            addindex: addindex,
        });
    },
    showPopup2() { 
        let that = this 
        if(that.data.title == ""||!that.data.title ){return} 
        this.setData({ showpop2: true,showadd1:false }) 
    },
    showaddf(){ this.setData({showadd1: true})},
    onClose() { this.setData({ showpop: false }) },
    onClose1() { this.setData({ del_index: -99 }) },
    closeadd(e) { 
        let that = this
        let del_index = e.currentTarget.dataset.index;
        let addindex = that.data.addindex
        addindex[del_index] = 0
        this.setData({ 
            addindex: addindex,
            // add_indexx: del_index
        });
    },
    onClose2() { this.setData({ showpop2: false }) },
    closeadd1(){ this.setData({ showadd1:false})},
    deletedata() {
        let that = this
        wx.showLoading({})
        wx.cloud.deleteFile({
            fileList:[that.data.dataList[that.data.del_indexx].fileIDs],
            success(res){
                console.log(res,'删除文件')
            },
            fail(err){
                wx.hideLoading({})
                console.log(err)
            }
        })
        db.collection('anmimg').doc(that.data.dataList[that.data.del_indexx]._id).remove({
            success(resp){
                console.log(that.data.del_indexx)
                that.data.dataList.splice(that.data.del_indexx, 1)
                that.setData({
                    dataList:that.data.dataList,
                    del_indexx: -99,
                    dataLength:that.data.dataLength-1
                })
                wx.hideLoading({})
                console.log(resp,'删除成功')
            },fail(resp){
                wx.hideLoading({})
                console.log(resp,'删除失败')
            }
        })
    },
    deletef() {
        let that = this
        wx.showLoading({})
        db.collection('folders').doc(that.data.foldersList[that.data.del_indexx]._id).remove({
            success(resp){
                console.log(that.data.del_indexx)
                that.data.foldersList.splice(that.data.del_indexx, 1)
                that.setData({
                    foldersList:that.data.foldersList,
                    del_indexx: -99,
                    foldersLength:that.data.foldersLength-1,
                    showpop:false
                })
                wx.hideLoading({})
                console.log(resp,'删除成功')
            },fail(resp){
                wx.hideLoading({})
                console.log(resp,'删除失败')
            }
        })
    },
    finish(){
        let folder = []
        let that = this
        wx.showLoading({
          title: '加载中',
        })
        for(let i = 0;i<that.data.dataList.length;i++){
            if(that.data.addindex[i]==1){
                folder.push(that.data.dataList[i])
            }
        }
        if(folder.length == 0){
            wx.showToast({
              title: '至少选择一个文件',
              icon:"none"
            })
            return
        }
        wx.cloud.database().collection("folders").add({
            data:{
                folder:folder,
                title:that.data.title
            },success(res){
                that.setData({
                    addindex:[],
                    showadd1:false,
                    showpop2:false
                })
                wx.reLaunch({
                  url: '../storage/index',
                })
                let f = {folder:folder,title:that.data.title}
                let newfolder = that.data.foldersList.concat(f)
               
                that.setData({foldersList:newfolder})
                console.log(that.data.foldersList)
                wx.hideLoading({
                    
                    success: (res) => {wx.showToast({
                      title: '成功',
                    })},
                  })
            }
        })
    },
    getInputValue(event){
        console.log("输入的内容", event.detail.value)
        this.setData({
            title: event.detail.value
        })
    },

    getFolderList(){
        let that = this
        wx.cloud.database().collection("folders").where({"_openid":app.globalData.openId}).get({
            success(res){
                console.log("chenggong",res)
                that.setData({
                    foldersList:res.data
                })
            }
        })
    },
    onLoad(options) {
        console.log(app.globalData.openId)
        this.getFolderList()
        this.setData({
            info: wx.getStorageSync('userInfo'),
            dataList: app.globalData.dataList,
            dataLength: app.globalData.dataLength,
            foldersLength: app.globalData.foldersLength,
            foldersList: app.globalData.foldersList
        })
    }, 
})